/**
 * ReportGear(2011)
 */
package com.reportgear.designer.pane.style;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JPanel;

import net.abbp.report.model.Cell;
import net.abbp.report.model.cell.style.HorizentalAlignment;
import net.abbp.report.model.cell.style.Style;
import net.abbp.report.model.cell.style.TextStyle;
import net.abbp.report.model.cell.style.VerticalAlignment;
import net.abbp.report.view.GridView;

import com.reportgear.core.swing.pane.base.JBasePane;

/**
 * @version 1.0 2011-3-24
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class AlignPane extends JBasePane implements ICellStyle {

    private JComboBox cbxHAlign;
    private JComboBox cbxVAlign;
    private JComboBox cbxTextStyle;

    public AlignPane() {
        super();

        initComponents();
    }

    private void initComponents() {
        setLayout(new BorderLayout());
        JPanel pane1 = new JPanel();
        pane1.setLayout(new GridLayout(1, 3, 2, 2));

        JPanel paneHAlign = new JPanel();
        paneHAlign.setLayout(new BorderLayout());
        JPanel paneVAlign = new JPanel();
        paneVAlign.setLayout(new BorderLayout());
        JPanel paneTextStyle = new JPanel();
        paneTextStyle.setLayout(new BorderLayout());

        paneHAlign.setBorder(BorderFactory.createTitledBorder("水平对齐"));
        cbxHAlign = new JComboBox(HorizentalAlignment.values());
        paneHAlign.add(cbxHAlign, BorderLayout.CENTER);

        paneVAlign.setBorder(BorderFactory.createTitledBorder("垂直对齐"));
        cbxVAlign = new JComboBox(VerticalAlignment.values());
        paneVAlign.add(cbxVAlign, BorderLayout.CENTER);

        paneTextStyle.setBorder(BorderFactory.createTitledBorder("文本控制"));
        cbxTextStyle = new JComboBox(TextStyle.values());
        paneTextStyle.add(cbxTextStyle, BorderLayout.CENTER);

        pane1.add(paneHAlign);
        pane1.add(paneVAlign);
        pane1.add(paneTextStyle);

        add(pane1, BorderLayout.NORTH);
    }

    public void populateCellStyle(GridView workSheet) {
        Style style = workSheet.getActiveCellStyle();

        this.cbxHAlign.setSelectedItem(style.getHorizentalAlignment());
        this.cbxVAlign.setSelectedItem(style.getVerticalAlignment());
        this.cbxTextStyle.setSelectedItem(style.getTextStyle());
    }

    public void updateCellStyle(GridView workSheet) {
        HorizentalAlignment hAlign = (HorizentalAlignment) this.cbxHAlign.getSelectedItem();
        VerticalAlignment vAlign = (VerticalAlignment) this.cbxVAlign.getSelectedItem();
        TextStyle textStyle = (TextStyle) this.cbxTextStyle.getSelectedItem();
        List<Cell> cells = workSheet.getSelectedCells();
        Style style = workSheet.getActiveCellStyle();
        if (style.getHorizentalAlignment() != hAlign) {
            for (Cell cell : cells) {
                cell.setStyle(cell.getStyle().derivetHorizentalAlignment(hAlign));
            }
        }

        if (style.getVerticalAlignment() != vAlign) {
            for (Cell cell : cells) {
                cell.setStyle(cell.getStyle().derivetVerticalAlignment(vAlign));
            }
        }

        if (style.getTextStyle() != textStyle) {
            for (Cell cell : cells) {
                cell.setStyle(cell.getStyle().derivetTextStyle(textStyle));
            }
        }
    }
}
